package com.xinbochuang.machine.admin.service.impl.macRoom;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.macRoom.Equipment;
import com.xinbochuang.machine.admin.domain.macRoom.EquipmentDetail;
import com.xinbochuang.machine.admin.dto.EquipmentDetailDto;
import com.xinbochuang.machine.admin.mapper.macRoom.EquipmentDetailMapper;
import com.xinbochuang.machine.admin.service.macRoom.IEquipmentDetailService;
import com.xinbochuang.machine.common.domain.TableDataInfo;

/**
 *
 * @author 杜文博
 * @date 2020-09-08
 */
@Service
public class EquipmentDetailServiceImpl extends ServiceImpl<EquipmentDetailMapper, EquipmentDetail> implements IEquipmentDetailService {

	@Autowired
	EquipmentDetailMapper equipmentDetailMapper;

	@Override
	public List<EquipmentDetailDto> findEquDetailByCabinetId(String cabinetId,String pageNum,String pageSize ) {

		return equipmentDetailMapper.findEquDetailByCabinetName(cabinetId,pageNum,pageSize);
	}

	@Override
	public Integer equDetailTotalCount(String cabinetId, String pageNum, String pageSize) {
		return equipmentDetailMapper.EquDetailTotalCount(cabinetId,pageNum,pageSize);
	}

	@Override
	public TableDataInfo findEquDetailByMacRoom(EquipmentDetail equipmentDetail) {
		QueryWrapper<EquipmentDetail> queryWrapper = new QueryWrapper<>();
		IPage<EquipmentDetail> list = page(new Page<>(equipmentDetail.getPageNum(), equipmentDetail.getPageSize()), queryWrapper);
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(HttpStatus.HTTP_OK);
		rspData.setMsg("查询成功");
		rspData.setRows(list.getRecords());
		rspData.setTotal(list.getTotal());
		return rspData;
	}

	@Override
	public int importEquipmentDetail(List<Map<String, Object>> maps) {
		EquipmentDetail repeatEqu = new EquipmentDetail();
		Integer result = 0;
//		//数组去空处理
//		List<Map<String,Object>> lists=maps.stream().filter(string -> !string.isEmpty()).collect(Collectors
//			.toList());
		if(maps.size()>0){
			for (Map<String, Object> map : maps) {
				EquipmentDetail equipmentDetail = new EquipmentDetail();
				for (String s : map.keySet()) {
					if(s.equals("机柜名称")&&map.get(s)!=null){
						String equCabinetName = map.get(s).toString().replaceAll(" ", "");
						equipmentDetail.setCabinetName(equCabinetName);
					}
					if(s.equals("资产标签号")&&map.get(s)!=null){
						equipmentDetail.setAssetsNumber(map.get(s).toString());
					}else if(s.equals("资产名称第一段（资产大类）")&&map.get(s)!=null){
						equipmentDetail.setAssetsBigType(map.get(s).toString());
					}
					else if(s.equals("资产名称第二段（资产小类）")&&map.get(s)!=null){
						equipmentDetail.setAssetsSmallType(map.get(s).toString());
					}
					else if(s.equals("归属系统")&&map.get(s)!=null){
						equipmentDetail.setOwnerSystem(map.get(s).toString());
					}
					else if(s.equals("规格型号")&&map.get(s)!=null){
						equipmentDetail.setModelType(map.get(s).toString());
					}
					else if(s.equals("资产状态")&&map.get(s)!=null){
						equipmentDetail.setAssetsState(map.get(s).toString());
					}
					else if(s.equals("责任人")&&map.get(s)!=null){
						equipmentDetail.setPrincipal(map.get(s).toString());
					}
					else if(s.equals("责任部门")&&map.get(s)!=null){
						equipmentDetail.setDepartment(map.get(s).toString());
					}
					else if(s.equals("科室")&&map.get(s)!=null){
						equipmentDetail.setOffice(map.get(s).toString());
					}
					else if(s.equals("机房名称")&&map.get(s)!=null){
						equipmentDetail.setMacRoomName(map.get(s).toString());
					}
					else if(s.equals("所属机柜编号")&&map.get(s)!=null) {
						equipmentDetail.setCabinetId(map.get(s).toString());
					}
					else if(s.equals("所属U位")&&map.get(s)!=null){
						equipmentDetail.setUfloorNum(map.get(s).toString());
					}
					else if(s.equals("启用日期")&&map.get(s)!=null){
						equipmentDetail.setOpenDate(map.get(s).toString());
					}
					else if(s.equals("机柜名称")&&map.get(s)!=null){
						equipmentDetail.setCabinetName(map.get(s).toString());
					}
					String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
					equipmentDetail.setUuid(uuid);
					equipmentDetail.setCreateTime(DateUtil.now().toString());
					equipmentDetail.setUpdateTime(DateUtil.now().toString());
					equipmentDetail.setDeleteFlag(0);
				}

				if(equipmentDetail.getMacRoomName()!=null&&equipmentDetail.getCabinetName()!=null&&equipmentDetail
					.getUfloorNum()!=null){
					//根据机房名称，机柜名称，所属U位查询对应设备
					List<Equipment> equList = equipmentDetailMapper.findEqupment(
						equipmentDetail.getMacRoomName(),
						equipmentDetail.getCabinetName(),
						equipmentDetail.getUfloorNum());
					//若设备存在，将设备Id插入设备详情表
					if(equList.size()>0){
						equipmentDetail.setEquId(equList.get(0).getUuid());
						equipmentDetail.setCabinetId(equList.get(0).getCabinetId());
						//若设备表Id已被资产绑定则不可重复绑定
						repeatEqu = equipmentDetailMapper.selectByEquId(equList.get(0).getUuid());
						if(repeatEqu!=null){
							continue;
						}
					}
					result =  equipmentDetailMapper.insert(equipmentDetail);
				}
			}
		}
		return result;
	}


}
